package com.android.kotlincoroutinemvvmdemo

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.kotlincoroutinemvvmdemo.adapter.CommonRecyclerViewPool
import com.android.kotlincoroutinemvvmdemo.adapter.TwoFiAdapter
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityRecyclerViewPoolBinding
import com.android.kotlincoroutinemvvmdemo.viewmodel.OneFiViewModel
import kotlinx.coroutines.launch

class RecyclerViewPoolActivity : BaseActivity<ActivityRecyclerViewPoolBinding>() {

    private lateinit var mAdapter: TwoFiAdapter

    private lateinit var mViewModel: OneFiViewModel

    override fun getLayoutId() = R.layout.activity_recycler_view_pool

    override fun init(savedInstanceState: Bundle?) {

        mViewModel = ViewModelProvider(this)[OneFiViewModel::class.java]

        mAdapter = TwoFiAdapter(this@RecyclerViewPoolActivity)
        mBinding.rvRecyclerPool.apply {
            layoutManager = LinearLayoutManager(this@RecyclerViewPoolActivity)
            adapter = mAdapter
            setRecycledViewPool(CommonRecyclerViewPool)
        }

        mViewModel.getOneFiDataSet()

        lifecycleScope.launch {
            mViewModel.mOneFiFlow.collect {
                mAdapter.setDataSet(it)
            }
        }

        Log.d(TAG,"recycler count:${CommonRecyclerViewPool.getRecycledViewCount(CommonRecyclerViewPool.ITEM_TYPE)}")
    }

    companion object {
        const val TAG = "RecyclerViewPoolActivity"
    }
}